<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login_model extends CI_Model {

    private $tbl_data = 'ACL_USER_LOGIN';

    public function __construct() {
        parent::__construct();
        $this->load->helper('ldap');
        $this->load->model('master/corporate_model'); #Location ./cmis/modules/master/models
    }

    public function get_corp_user($username) {
        $sql = "SELECT B.FCCODE,B.FCCODE+' ['+B.FCNAME+']' FCNAME   FROM  ACL_USER_CORPORATE A
					INNER JOIN  DTS_CORPORATE B ON A.FCCODE=B.FCCODE WHERE A.USERNAME='" . $username . "' ORDER BY B.FCNAME";
        return $this->db->query($sql)->result_array();
        /* $trow=	count($res);
          $list 	= 	NULL;
          if (count($res)>1)	$list .= "<option value=''>--��س����͡--</option>\n";
          for($x=0;$x<$trow;$x++)
          {
          $list .= "<option value='".$res[$x]['FCCODE']."'>".$res[$x]['FCNAME']."</option>\n";
          }
          print_r($list); */
    }

    public function checkAuth2($uName, $pass, $corporate) {
        $ldap = new ldap();
        $ldap->s_User($uName);
        $ldap->s_Pass($pass);
        $server = "172.16.1.32"; //dc1-nu
        $con = @ldap_connect($server, 636);
        ldap_set_option($con, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($con, LDAP_OPT_REFERRALS, 0);
        $user = $uName . "@lpn.co.th";
        // connect to active directory
        $ad = ldap_connect($server);
        if (!$ad) {
            return "�������ö�Դ��� server ���͵�Ǩ�ͺ���ʼ�ҹ��";
        }
        $b = @ldap_bind($ad, $user, $pass);
        if ($b) {
            $this->db->where('username', $uName);
            $result = $this->db->get($this->tbl_data)->row();
            if ($result->STATUS == 'Disabled') {
                return "�������ö�������к��� ��سҵԴ��ͼ������к�";
            }
            $this->upate_last_login($uName);
            $my_resource = $this->get_my_resource($uName);
            $my_corporate = $this->corporate_model->get_by_id($corporate);
            $request_url = array();
            foreach ($my_resource as $row) {
                $request_url[$row->controller] = $row;
            }
            $sessionArray = array(
                'username' => $uName,
                'fullname' => $result->FIRST_NAME + ' ' + $result->LAST_NAME,
                'logged_in' => TRUE,
                'corporate' => $my_corporate,
                'my_resource' => $my_resource,
                'request_url' => $request_url
            );
            $this->session->set_userdata($sessionArray);
            return "connected";
        } else {
            return "��ҹ��͡���ʼ�ҹ�Դ ��س��ͧ����";
        }
    }

    public function checkAuth($uName, $pass, $corporate) {
        $this->db->where('username', $uName);
        $result = $this->db->get($this->tbl_data)->row();
        if ($result->STATUS == 'Disabled') {
            return "�������ö�������к��� ��سҵԴ��ͼ������к�";
        }
        $this->upate_last_login($uName);
        $my_resource = $this->get_my_resource($uName);
        $my_corporate = $this->corporate_model->get_by_id($corporate);
        $request_url = array();
        foreach ($my_resource as $row) {
            $request_url[$row->controller] = $row;
        }
        $sessionArray = array(
            'username' => $uName,
            'fullname' => $result->FIRST_NAME + ' ' + $result->LAST_NAME,
            'logged_in' => TRUE,
            'corporate' => $my_corporate,
            'my_resource' => $my_resource,
            'request_url' => $request_url
        );
        $this->session->set_userdata($sessionArray);
        return "connected";
    }

    public function upate_last_login($username) {
        $this->db->where('username', $username);
        $this->db->set('last_login', 'getdate()', FALSE);
        $this->db->update($this->tbl_data);
    }

    public function get_my_resource($username) {
        $sql = "SELECT  b.order_no,a.recode,b.parent_id,b.name,b.controller,b.isreport,
					sum(a.can_edit) can_edit,sum(a.can_access)  can_access,
					sum(a.can_delete) can_delete,sum(a.can_export) can_export,
					sum(a.can_approve) can_approve
					FROM acl_permissions a
					INNER JOIN acl_resources b ON a.recode=b.recode
					WHERE a.rocode IN (SELECT rocode FROM acl_user_roles  WHERE username='" . $username . "') 
					GROUP BY  b.order_no,a.recode,b.parent_id,b.name,b.controller,b.isreport ORDER BY b.order_no";
        return $this->db->query($sql)->result();
    }

    public function check_session() {
        if ($this->session->userdata('username') AND $this->session->userdata('logged_in') == 1):
            return TRUE;
        else:
            return FALSE;
        endif;
    }

    public function log_message($logArray) {
        if (isset($logArray)):
            $this->db->insert('your_log', $logArray);
        endif;
    }

}

/* End of file login_model.php */
/* Location: ./application/models/login_model.php */